Skip to content

fix(agent-core): classify context-worded 401/403 as context.overflow instead of auth error - #1

Closed
bowser-bot wants to merge 1 commit into
mainfrom
fix/context-overflow-401-classification
Closed

fix(agent-core): classify context-worded 401/403 as context.overflow instead of auth error#1
bowser-bot wants to merge 1 commit into
mainfrom
fix/context-overflow-401-classification

Conversation

@bowser-bot

@bowser-bot bowser-bot commented Aug 4, 2026

Copy link
Copy Markdown
Owner

WIP — not ready for upstream. This draft lives only in this fork for development and review. Do not submit to MoonshotAI/kimi-code in the current state.

Related Issue

Related to MoonshotAI#2613

Problem

The Kimi managed subscription intentionally returns 401/403 for capability rejections worded as <model> supports only <N> context. The client treated every 401 as a credential failure, so an over-long context (e.g. an auto-compaction request that itself exceeds a 256K model's window) forced a pointless token refresh and surfaced as -32000 Authentication required over ACP — indistinguishable from a real auth problem.

What changed

The 401/403 detection is added only in the Kimi vendor error module (kosong/src/providers/kimi-errors.ts, beside the existing classifyKimiQuotaError), with a quantity-anchored wording gate: a size must follow "supports only" (256K context, kimi-k3 up to 256K context). Auth/scope rejections that merely mention "context" in prose keep the credential path, and the shared provider-agnostic classification is untouched — nothing changes for non-Kimi providers.

  • v1 OAuth wrappers (agent-core/session/provider-manager.ts, node-sdk/kimi-code-model-provider.ts): skip the forced token refresh and throw CONTEXT_OVERFLOW with the provider's message instead of a misleading re-login prompt. errors/serialize.ts maps the same wording to the context.overflow payload code.
  • v2 parity (agent-core-v2): the model requester skips the forced refresh and re-mints the error as APIContextOverflowError; classifyApiError reports context_overflow, not auth.
  • Compaction (agent-core/compaction/full.ts): propagates context.overflow unwrapped (matching the existing auth-code passthrough) and treats its KimiError form as recoverable, keeping the shrink-and-retry path.
  • ACP wire (acp-adapter/session.ts): a turn.ended failure coded context.overflow now rejects session/prompt with -32603 and structured data { code: "context.overflow" } instead of silently resolving end_turn. Only whitelisted public codes cross as data.code; raw provider messages and stacks never cross the wire. Other non-auth codes keep the log-and-resolve path.
  • Telemetry (agent-core/turn/index.ts): checks the Kimi-scoped wording before the auth statuses; ordering otherwise unchanged.

Out of scope (awaiting maintainer feedback on the issue): session/set_model pre-flight checks and chunking oversized compaction input. Wordings without a quantity are not matched by design; the Kimi-scoped pattern is the single extension point.

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue, or explained the problem above.
  • I have added tests that prove my feature works.
  • Ran gen-changesets skill, or this PR needs no changeset. — changeset added (minor, both publishable packages; wire-visible behavior semantics change)
  • Ran gen-docs skill, or this PR needs no doc update. — behavior fix only, no config/API surface change

Verification

  • Targeted suites (8 files) — 312 passed, 1 skipped
  • Full regression: kosong + acp-adapter + node-sdk — 2046 passed; agent-core — 4132 passed; agent-core-v2 — 4676 passed, 11 failed (plugin zip/GitHub install fixtures that fail identically on the base commit in this sandbox — unrelated)
  • pnpm typecheck clean; oxlint 0 errors on changed files
  • End-to-end: OAuth provider + synthetic 401 k3-256k supports only 256K context. → no forced refresh, turn fails context.overflow, ACP client receives -32603 + data.code; genuine-auth negatives (Bearer/scope phrasings) still refresh and stay auth errors.

@bowser-bot bowser-bot closed this Aug 4, 2026
@bowser-bot bowser-bot reopened this Aug 4, 2026
@bowser-bot bowser-bot closed this Aug 4, 2026
@bowser-bot bowser-bot reopened this Aug 4, 2026
…xt.overflow

The Kimi managed subscription intentionally returns 401/403 for
capability rejections worded as "<model> supports only <N> context".
Treating every 401 as a credential failure forced a pointless token
refresh and surfaced "Authentication required" for an over-long
context, both over ACP and in the TUI.

- kosong: add isKimiContextCapabilityError in the Kimi vendor error
  module (providers/kimi-errors.ts, next to classifyKimiQuotaError),
  with a quantity-anchored wording gate so auth/scope rejections that
  merely mention "context" stay on the credential path; the shared
  provider-agnostic classification is untouched, so non-Kimi providers
  are unaffected
- agent-core / node-sdk OAuth wrappers: skip the forced token refresh
  on these errors and throw CONTEXT_OVERFLOW with the provider's
  message instead of a misleading re-login prompt
- agent-core serialize: map the same wording on 401/403 to the
  context.overflow payload code instead of provider.auth_error
- compaction: propagate context.overflow unwrapped (matching the
  existing auth-code passthrough) and treat its KimiError form as
  recoverable so shrink-and-retry still applies
- acp-adapter: a turn.ended failure coded context.overflow now rejects
  session/prompt with -32603 carrying the code as structured data
  instead of silently resolving end_turn; wire data codes are limited
  to a public whitelist and raw provider messages/stacks never cross
  the wire
- agent-core-v2 parity: the model requester skips the forced refresh
  and re-mints Kimi context-worded 401s as APIContextOverflowError;
  classifyApiError classifies them as context_overflow
- turn telemetry: check the Kimi-scoped wording before the auth
  statuses; baseline status ordering otherwise unchanged
@bowser-bot
bowser-bot force-pushed the fix/context-overflow-401-classification branch from 3958e0e to 36d57be Compare August 4, 2026 18:15
@bowser-bot bowser-bot closed this Aug 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant